package com.feihong.txgw.tx_precenter;

import com.feihong.txgw.greendao.gen.ShopCarGoodsModelDao;
import com.feihong.txgw.inter.contract.product.IProListContract;
import com.feihong.txgw.json.goodsCart.GoodsCartParser;
import com.feihong.txgw.json.goodsCart.TerminalListParser;
import com.feihong.txgw.message.UpdateCartMessage;
import com.feihong.txgw.model.greendao.ShopCarGoodsModel;
import com.feihong.txgw.shopcar.bean.TerminalEntiyt;
import com.feihong.txgw.tx_model.ProListMainModel;
import com.feihong.txgw.utils.ToastUtils;
import com.feihong.txgw.utils.greendao.GreenDaoOperateHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName ProListMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/8 12:42
 * @Describe
 * @Change
 */
public class ProListMainPrecenter extends IProListContract.AbProListMainPrecenter {
    private int mCurrentPageNum = 1;
    boolean isFullPage = true;

    @Override
    public void getTerminalGoods(String storeID) {
        int mPageNum = mCurrentPageNum;
        mIModel.getTerminalGoods("10", storeID, String.valueOf(mPageNum)).subscribe(new Observer<TerminalListParser>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(TerminalListParser result) {
                if (result.getCode() == 200 && result.getData() != null) {
                    if (isFullPage && mCurrentPageNum <= 1) {
                        isFullPage = result.getData().getList().size() > 5;
                    }
                    mCurrentPageNum++;
                    mIView.getTerminalGoodsResult(result, isFullPage);
                } else {
                    mIView.loadEnd();
                }
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getTerminalFirstGoods(String storeID) {
        mCurrentPageNum = 1;
        isFullPage = true;
        getTerminalGoods(storeID);
    }

    /**
     * 请求服务器
     *
     * @param goods
     */
    private void realAddGoodsToShopCar(List<ShopCarGoodsModel> goods) {
        mIModel.realAddGoodsToShopCar(goods).subscribe(new Observer<GoodsCartParser>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(GoodsCartParser goodsCartParser) {
                parseShopCarResult(goodsCartParser);
            }

            @Override
            public void onError(Throwable e) {
                mIView.addGoodsToShopCarError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void addGoodsToShopCar(TerminalEntiyt terminalEntiyt) {
        mIModel.addGoodsToShopCar(terminalEntiyt).subscribe(new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                if (o instanceof String) {
                    String msg = (String) o;
                    mIView.showSystemToast(msg);
                } else {
                    ShopCarGoodsModel model = (ShopCarGoodsModel) o;
                    if (mIView.isUserLogin()) {
                        ArrayList<ShopCarGoodsModel> list = new ArrayList<>();
                        list.add(model);
                        realAddGoodsToShopCar(list);
                    } else {
                        saveGoodsToLocal(model);
                    }
                }
            }
        });

    }

    /**
     * 添加商品到本地购物车
     *
     * @param model
     */
    private void saveGoodsToLocal(ShopCarGoodsModel model) {
        int localNum = 0;
        List<ShopCarGoodsModel> shopCarGoodsModels = GreenDaoOperateHelper.getModel(ShopCarGoodsModelDao.Properties.GoodsID, String.valueOf(model.getGoodsID()));

        ShopCarGoodsModel tempModel = (shopCarGoodsModels != null && shopCarGoodsModels.size() != 0) ? shopCarGoodsModels.get(0) : null;
        if (tempModel != null) {
            model.setId(tempModel.getId());
            localNum = tempModel.getGoodsNum();
            //数量
            model.setGoodsNum(model.getGoodsNum() + localNum);
        }
        GreenDaoOperateHelper.updateGoods(model);
        ToastUtils.showAddShopCarSuccessToast(mIView.getCurrentActivity());
        EventBus.getDefault().post(new UpdateCartMessage());
    }

    public static ProListMainPrecenter create() {
        return new ProListMainPrecenter();
    }

    /**
     * 添加商品到服务器购物车 返回值解析
     *
     * @param goodsCartParser
     */
    private void parseShopCarResult(GoodsCartParser goodsCartParser) {
        List<GoodsCartParser.DataBean> items = goodsCartParser.getData();
        if (goodsCartParser.getCode() == 200 && items != null) {
            GreenDaoOperateHelper.delAll();
            GreenDaoOperateHelper.addGoodsCast(items);
            EventBus.getDefault().post(new UpdateCartMessage());
            ToastUtils.showAddShopCarSuccessToast(mIView.getCurrentActivity());
        } else {
            mIView.showSystemToast(goodsCartParser.getMsg());
        }
    }

    @Override
    public IProListContract.IProListMainModel getModel() {
        return ProListMainModel.create();
    }

    @Override
    public void onStart() {

    }
}
